package com.yy.job.console.service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yy.job.common.dao.YYJobLogDOMapper;
import com.yy.job.common.dao.YYJobStepDOMapper;
import com.yy.job.common.dao.po.YYJobLogDO;
import com.yy.job.common.dao.po.YYJobLogDOExample;
import com.yy.job.common.dao.po.YYJobStepDO;
import com.yy.job.common.dao.po.YYJobStepDOExample;
import com.yy.job.console.converter.JobLogViewConverter;
import com.yy.job.console.dto.JobLogView;
import com.yy.job.console.dto.JobStepLogView;

@Service
public class JobLogConsoleService {
	
	@Autowired
	private YYJobLogDOMapper jobLogDOMapper;
	
	@Autowired
	private YYJobStepDOMapper jobStepDOMapper;
	
	@Autowired
	private JobStepLogConsoleService jobStepLogConsoleService;

	public List<JobLogView> listJobLogs(Long jobId) {
		// 查询log日志
		YYJobLogDOExample example = new YYJobLogDOExample();
		example.createCriteria().andJobIdEqualTo(jobId).andIsDeletedEqualTo("N");
		example.setOrderByClause("gmt_created asc");
		List<YYJobLogDO> jobStepLogList = jobLogDOMapper.selectByExample(example);

		List<JobLogView> listConvert = JobLogViewConverter.listConvert(jobStepLogList);
		// 查询步骤列表
		YYJobStepDOExample stepDOExample = new YYJobStepDOExample();
		stepDOExample.createCriteria().andJobIdEqualTo(jobId);
		List<YYJobStepDO> jobStepList = jobStepDOMapper.selectByExample(stepDOExample);
		Map<Long, YYJobStepDO> jobStepMap = jobStepList.stream().collect(Collectors.toMap(k->k.getId(), v->v));
		
		// 查询step日志
		for(JobLogView jobLogView : listConvert) {
			List<JobStepLogView> listJobStepLogs = jobStepLogConsoleService.listJobStepLogs(jobLogView.getId(), jobStepMap);
			jobLogView.setListJobStepLogs(listJobStepLogs);
		}
		
		return listConvert;
	}

}
